package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResChoiceRechargeEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductRebateEntity;
import com.taomu.app.zongchouhuli.http.res.ResRechargeEntity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserRechargeContract;
import com.taomu.app.zongchouhuli.moudle.mine.entity.PayWayEntity;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class UserRechargePresenter implements UserRechargeContract.Presenter {

    private UserRechargeContract.View mContractView;

    private CompositeDisposable disposable;

    public UserRechargePresenter(UserRechargeContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getPayWayData() {
        setPayWay();
    }

    @Override
    public void getRechargeInfo() {
        Request.getIntence().getChoiceRechargeInfo()
                .flatMap(new Function<String, ObservableSource<BaseChangeResponse<ResChoiceRechargeEntity>>>() {
                    @Override
                    public ObservableSource<BaseChangeResponse<ResChoiceRechargeEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResChoiceRechargeEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResChoiceRechargeEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResChoiceRechargeEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResChoiceRechargeEntity> baseResponse) {
                        if(mContractView != null){
                            mContractView.setPayWayInfo(baseResponse.getData());
                        }
                    }
                });
    }

    @Override
    public void getSubmit(String moeny, String status) {
        Request.getIntence().getSubmitRechargeOrder(moeny,status)
                .flatMap(new Function<String, ObservableSource<BaseChangeResponse<ResRechargeEntity>>>() {
                    @Override
                    public ObservableSource<BaseChangeResponse<ResRechargeEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResRechargeEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResRechargeEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResRechargeEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResRechargeEntity> baseResponse) {
                        if(mContractView != null){
                            mContractView.setSubmit(baseResponse.getData());
                        }
                    }
                });

    }

    @Override
    public void detch() {

    }

    String[] payways = {"商户充值","支付宝扫码支付","支付宝充值","微信充值","银联充值"};

    private void setPayWay(){
        List<PayWayEntity> mData = new ArrayList<>();
        for(int i=0;i<payways.length;i++){
            PayWayEntity entity = new PayWayEntity();
            entity.setTitle(payways[i]);
            entity.setTypeid(String.valueOf(i));
            mData.add(entity);
        }
        if(mContractView != null){
            mContractView.setPayWayData(mData);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
